import {
    TileSuperMapRest,
    VectorTileSuperMapRest,
    MapboxStyles,
    FeatureService,
    GetFeaturesByGeometryParameters,
} from "@supermap/iclient-ol";
import Polygon from "ol/geom/Polygon";
import {
    Style, Icon, Circle
    , Fill
} from "ol/style";
import { GeoJSON } from "ol/format";

import markerbig_select from "@/assets/markerbig_select.png"


// 读取url中的 根节点， 数据源名称，  数据源名称
export function getDataByUrl(url) {
    const arr = url.split("/")
    const dataSourceName = arr[arr.length - 3]
    const datasets = arr[arr.length - 1]
    const datasetName = dataSourceName + ":" + datasets
    arr.length = arr.length - 4
    const baseUrl = arr.join('/')
    return {
        dataSourceName,
        datasets,
        datasetName,
        baseUrl
    }
}

// 获取Features数据
export function getFeaturesObjByGeometry(baseUrl, datasetName) {
    const featureService = new FeatureService(baseUrl);
    var polygon = new Polygon([]);
    var getFeatureParams = new GetFeaturesByGeometryParameters({
        toIndex: -1,
        datasetNames: [datasetName],
        geometry: polygon,
        spatialQueryMode: "NONE",
        // returnContent: true,
        targetEpsgCode: "4326",
    });
    return featureService
        .getFeaturesByGeometry(getFeatureParams)
        .then(serviceResult => {
            return serviceResult.result.features
        })
}

export function getVectorStyle() {
    return new Style({
        image: new Icon(({
            anchor: [0.5, 0.9],
            src: markerbig_select
        }))
    })
}

export function loadVector(vectorSource, featuresObj, style) {
    const features = (new GeoJSON()).readFeatures(featuresObj)
    for (var i = 0; i < features.length; i++) {
        features[i].setStyle(style);
    }
    if (vectorSource.getFeatures().length > 0) {
        vectorSource.clear();
    }
    vectorSource.addFeatures(features);

    return features
}